//
//  EventManagementViewController.m
//  Runone
//
//  Created by runone02 on 16/12/5.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "EventManagementViewController.h"
#import "OneConst.h"
#import "AppConstants.h"
#import "EventListViewController.h"
#import "UIView+Addition.h"
//#import "EventReportTableViewController.h"
#import "EvetnSearchViewController.h"
#import "RightCodeTool.h"
#import "OtherEventsReportedViewController.h"
#import "ChargeReportedViewController.h"
#import "ConstructionEventReportViewController.h"
#import "CurrentEventSearchController.h"
#import "HistorySearchViewController.h"
#import "StationEventSearchController.h"
#import "EvetnSearchViewController.h"
#import "OtherEventsReportedViewController.h"
#import "GLTollStationDataListViewController.h"
#import "ChargeDataSubmitController.h"
#import "EKPopMenuView.h"
#import "SVProgressHUD.h"
#import "GLCurrentEventListViewController.h"
#import "GLHistoryEventViewController.h"
#import "GLTollStationListViewController.h"
#import "Define.h"

#define kScreenWidth    [UIScreen mainScreen].bounds.size.width
#define ROScreenW [UIScreen mainScreen].bounds.size.width
#define ROScreenH [UIScreen mainScreen].bounds.size.height
@interface EventManagementViewController ()<UIPopoverPresentationControllerDelegate>

//@property (nonatomic, strong) EventReportTableViewController *eventPop;
@property (nonatomic, strong) NSArray *vcArray;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *vcDataArray;
@property (nonatomic, strong) EKPopMenuView *dateMenuV;
@property (nonatomic, assign) BOOL isSelectAddEventBtn;

@property (nonatomic,strong) UIBarButtonItem *historyBtn;


@end

@implementation EventManagementViewController

#pragma    -----------事件管理主界面


- (void) resetNavigationBar:(NSInteger)type {
    switch (type) {
        case 0:
            
            break;
        case 1:
            
            break;
        case 2:
            
            break;
        case 3:
            
            break;
            
        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
    [self configDropDownData];
    [self setUpNavigationBar];
    [self setupView];
    [self addAllChildVc];
//     RegisterNotify(GLCurrentEventShowNotification, @selector(currentChangeNaviBar));
    [self currentChangeNaviBar];
    
}
//设置滚动范围
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / ROScreenW;
    UIViewController *vc = super.childViewControllers[i];
    super.selIndex = i;
    UILabel *label = self.titleLabels[i];
     [super selectLabel:label];
    if ([vc isKindOfClass:[GLCurrentEventListViewController class]] || [vc isKindOfClass:[GLTollStationDataListViewController class]]) {
        _historyBtn.image =nil;
        _historyBtn.action = nil;
    }else {
        _historyBtn.image =[UIImage imageNamed:@"search_icon"];
        _historyBtn.action = @selector(searchBtnClick);
    }
    // 计算下最大的标题视图滚动区域
    CGFloat offsetX = label.center.x - ROScreenW * 0.5;
    
    if (offsetX < 0) {
        offsetX = 0;
    }
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = super.titleScrollView.contentSize.width - ROScreenW + super.titleMargin;
    
    if (maxOffsetX < 0) {
        maxOffsetX = 0;
    }
    
    if (offsetX > maxOffsetX) {
        offsetX = maxOffsetX;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}


// 标题按钮点击
- (void)titleClick:(UITapGestureRecognizer *)tap {
    // 记录是否点击标题
    
    super.isClickTitle = YES;
    
    // 获取对应标题label
    UILabel *label = (UILabel *)tap.view;
    
    // 获取当前角标
    NSInteger i = label.tag;
    
    // 选中label
    [super selectLabel:label];
    
    // 内容滚动视图滚动到对应位置
    CGFloat offsetX = i * ROScreenW;
    
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 记录上一次偏移量,因为点击的时候不会调用scrollView代理记录，因此需要主动记录
    super.lastOffsetX = offsetX;
    [self selectLabel:label];
    // 添加控制器
    UIViewController *vc = super.childViewControllers[i];
    
     super.selIndex = i;
    
    if ([vc isKindOfClass:[GLCurrentEventListViewController class]] || [vc isKindOfClass:[GLTollStationDataListViewController class]]) {
        _historyBtn.image =nil;
        _historyBtn.action = nil;
    }else {
        _historyBtn.image =[UIImage imageNamed:@"search_icon"];
        _historyBtn.action = @selector(searchBtnClick);
    }
    
    // 计算下最大的标题视图滚动区域
    CGFloat offsetXTitle = label.center.x - ROScreenW * 0.5;
    
    if (offsetXTitle < 0) {
        offsetXTitle = 0;
    }
    // 计算下最大的标题视图滚动区域
    CGFloat maxOffsetX = super.titleScrollView.contentSize.width - ROScreenW + super.titleMargin;
    
    if (maxOffsetX < 0) {
        maxOffsetX = 0;
    }
    
    if (offsetXTitle > maxOffsetX) {
        offsetXTitle = maxOffsetX;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetXTitle, 0) animated:YES];
    
    // 点击事件处理完成
    super.isClickTitle = NO;
}




//设置导航栏
- (void) currentChangeNaviBar {
    
    _historyBtn =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search_icon"] style: UIBarButtonItemStyleDone target:self action:@selector(searchBtnClick)];
//    _historyBtn.image =nil;
//    _historyBtn.action = nil;
    
    UIBarButtonItem *mainAndSearchBtn =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus_icon"] style: UIBarButtonItemStyleDone target:self action:@selector(addIncidentBtnClick:)];
    self.navigationItem.rightBarButtonItems = @[mainAndSearchBtn,_historyBtn];
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self configDropDownData];
}

//配置加号按钮UI
- (void) configDropDownData {
    
    _dataSource = [[NSMutableArray alloc] init];
    _vcDataArray = [[NSMutableArray alloc] init];
    ConstructionEventReportViewController *ccVC=[[ConstructionEventReportViewController alloc] init];
    ChargeReportedViewController     *chargeVC=[[ChargeReportedViewController alloc] init];
    ChargeDataSubmitController       *dataSubmitVc = [[ChargeDataSubmitController alloc] init];
    OtherEventsReportedViewController *OtherVC=[[OtherEventsReportedViewController alloc] init];
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P040403"]) {
        [_dataSource addObject:@"施工事件上报"];
        [_vcDataArray addObject:ccVC];
    }
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P040405"]) {
        [_dataSource addObject:@"其他事件上报"];
        [_vcDataArray addObject:OtherVC];
    }
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P040404"]) {
        [_dataSource addObject:@"收费事件上报"];
        [_vcDataArray addObject:chargeVC];
    }
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P040502"]) {
        [_dataSource addObject:@"收费数据上报"];
        [_vcDataArray addObject:dataSubmitVc];
    }
}



//设置标题
- (void) setUpNavigationBar {
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:(68.0 / 255) green:(169.0 / 255) blue:(213.0 / 255) alpha:1.000]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel *titleLabel            = [[UILabel alloc] init];
    titleLabel.size                = CGSizeMake(100, 30);
    titleLabel.text                = @"事件管理";
    titleLabel.textAlignment       = NSTextAlignmentCenter;
    titleLabel.textColor           = [UIColor whiteColor];
    self.navigationItem.titleView  = titleLabel;
//    UIBarButtonItem *mainAndSearchBtn =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus_icon"] style: UIBarButtonItemStyleDone target:self action:@selector(addIncidentBtnClick:)];
//    self.navigationItem.rightBarButtonItems = @[mainAndSearchBtn];
    
}

#pragma mark ------------------------ 加号按钮点击（事件上报）------------------
- (void)addIncidentBtnClick:(UIButton *)sender {
    _isSelectAddEventBtn = !_isSelectAddEventBtn;
    if (_isSelectAddEventBtn) {
        _dateMenuV = [[EKPopMenuView alloc] initEKPopViewWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 150 -8,0,150, 44 *_dataSource.count) annimationFormLeft:NO];
        
        _dateMenuV.itemNameArray = _dataSource;
        _dateMenuV.fontSize  = 16.0f;
        _dateMenuV.fontColor = [UIColor whiteColor];
        _dateMenuV.itemBgColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8];
        [_dateMenuV dismissHandler:^(BOOL isCanceled, NSInteger row) {
            if (!isCanceled) {
                UIViewController *vc = _vcDataArray[row];
                [self.navigationController pushViewController:vc animated:YES];
            }
            _isSelectAddEventBtn = NO;
        }];
        [_dateMenuV show];
        
    }else {
        [_dateMenuV hideWithAnimation:YES];
    }
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

#pragma mark -搜索按钮点击
- (void)searchBtnClick {
    if (_dateMenuV != nil) {
        [_dateMenuV hideWithAnimation:YES];
    }
    switch (self.currentVCOnIndex) {
           
        case 0:{
            CurrentEventSearchController *vc0 = [[CurrentEventSearchController alloc] init];
            vc0.titleType  = EventListTitleTypeCurrent;
            [self.navigationController pushViewController:vc0 animated:YES];
        }
            break;
        case 1:{
            HistorySearchViewController *vc1 = [[HistorySearchViewController alloc] init];
            vc1.titleType = EventListTitleTypeHistory;
            [self.navigationController pushViewController:vc1 animated:YES];
        }
            break;
        case 2:{
            StationEventSearchController *vc2 = [[StationEventSearchController alloc] init];
            vc2.titleType  = EventListTitleTypeToll;
            [self.navigationController pushViewController:vc2 animated:YES];
        }
            break;
        default:{
            CurrentEventSearchController *vc3 = [[CurrentEventSearchController alloc] init];
            vc3.titleType  = EventListTitleTypeConstruct;
            [self.navigationController pushViewController:vc3 animated:YES];
        }
            break;
    }
    
}

#pragma mark -设置四个标题的控制器
- (void)setupView {
    // 设置标题栏样式
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight) {
        *titleScrollViewColor = [UIColor whiteColor];
        *norColor = [UIColor darkGrayColor];
        *selColor = [UIColor colorWithRed:(68.0 / 255) green:(169.0 / 255) blue:(213.0 / 255) alpha:1.000];
        *titleHeight = EventTitlesViewH;
    }];
    // 设置下标
    [self setUpUnderLineEffect:^(BOOL *isShowUnderLine, BOOL *isDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor) {
        
        *isShowUnderLine = YES;
        *underLineH      = 2;
        *underLineColor = [UIColor colorWithRed:(68.0 / 255) green:(169.0 / 255) blue:(213.0 / 255) alpha:1.000];
    }];
}
//布局所有子控件
- (void)addAllChildVc {
    
    NSMutableArray *arrayVC = [[NSMutableArray alloc] init];
    GLCurrentEventListViewController *currentVc = [[GLCurrentEventListViewController alloc]init];
    currentVc.title = @"当前事件";
    currentVc.titleType = EventListTitleTypeCurrent;
    currentVc.methodName =@"GetAllCurrentEventByPage";
    
    GLHistoryEventViewController *historyVc = [[GLHistoryEventViewController alloc]init];
    historyVc.title = @"历史事件";
    historyVc.titleType = EventListTitleTypeHistory;
    historyVc.methodName = @"GetAllHistoryEventByPage";
    
    [arrayVC addObject:currentVc];
    [arrayVC addObject:historyVc];
    
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P0403"]) {
        GLTollStationListViewController *tollVc  = [[GLTollStationListViewController alloc] init];
        tollVc.title = @"收费事件";
        tollVc.titleType = EventListTitleTypeToll;
        tollVc.methodName = @"GetAllTollStationEventByPage";
        [arrayVC addObject:tollVc];
    }
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P0405"]) {
        GLTollStationDataListViewController *stationDataVc = [[GLTollStationDataListViewController alloc] init];
        stationDataVc.title = @"收费数据";
        //_stationDataVc.methodName = @"";
        [arrayVC addObject:stationDataVc];
    }
    if ([[RightCodeTool sharedTool] rightIsEqualTO:@"P0404"] ) {
        EventListViewController *constructVc = [[EventListViewController alloc] init];
        constructVc.title = @"施工事件";
        constructVc.titleType = EventListTitleTypeConstruct;
        constructVc.methodName = @"GetAllConstructEventByPage";
        [arrayVC addObject:constructVc];
    }
    
    for (UIViewController *VC in arrayVC) {
        [self addChildViewController:VC];
    }
    
    
}

@end
